package kam.fire;

import java.io.BufferedInputStream;
import java.io.File;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

/**
 * the FullScreenController class is used to display images at fullscreen.
 * 
 * @author palepail
 * 
 */
public class FullScreenController extends Activity {
	KamfireInstance kamfireInstance;
	private String imageUrl;
	private String imageType;
	private int imageSize;
	private File imageFile = null;

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindDrawables(findViewById(R.id.fullscreenlayout));
		System.gc();
	}

	private void unbindDrawables(View view) {
		LinearLayout fullScreen = (LinearLayout) view;
		if (fullScreen.getBackground() != null) {
			fullScreen.getBackground().setCallback(null);
		}

		fullScreen.removeAllViewsInLayout();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fullscreen);

		Bundle imageBundle = getIntent().getExtras();
		imageType = (String) imageBundle.getSerializable("IMAGE_TYPE");
		String campsite = (String) imageBundle.getSerializable("USER_CAPSITE");
		String apiToken = (String) imageBundle.getSerializable("USER_APITOKEN");
		kamfireInstance = new KamfireInstance(campsite, apiToken);

		imageFile = (File) imageBundle.getSerializable("IMAGE_FILE");
		if (imageFile != null) {
			if (imageType.equalsIgnoreCase("image/gif")||imageType.equalsIgnoreCase(".gif")) {
				loadGifFromFile(imageFile);
				renderGif();
			} else {
				displayFile(imageFile);
			}
		} else {
			if (imageType != null && imageType.equals("image/gif")) {
				imageUrl = (String) imageBundle.getSerializable("IMAGE_URL");
				imageSize = (Integer) imageBundle.getSerializable("IMAGE_SIZE");
				new LoadGif().execute();
			} else {
				LinearLayout layout = (LinearLayout) findViewById(R.id.fullscreenlayout);
				TouchImageView imageView = new TouchImageView(this);
				layout.addView(imageView);
				imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
						LayoutParams.MATCH_PARENT));
				imageView.setImageBitmap(kamfireInstance.loadUnscaledImageFromWeb(imageUrl));
				imageView.setOnClickListener(imageListener);
			}

		}

	}

	private void displayFile(final File downloadedFile) {
		LinearLayout layout = (LinearLayout) findViewById(R.id.fullscreenlayout);
		TouchImageView imageView = new TouchImageView(this);
		layout.addView(imageView);
		imageView.setImageBitmap(BitmapFactory.decodeFile(downloadedFile.getAbsolutePath()));
		imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		imageView.setOnClickListener(imageListener);

	}

	private void renderGif() {
		LinearLayout layout = (LinearLayout) findViewById(R.id.fullscreenlayout);
		layout.setGravity(Gravity.CENTER);
		BufferedInputStream stream = null;
		stream = kamfireInstance.getGifStream();
		GIFView view = new GIFView(this, stream);
		layout.addView(view);
		view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		view.setOnClickListener(imageListener);
	}

	private void loadGif() {
		kamfireInstance.setGifStream(imageUrl, imageSize);
	}

	private void loadGifFromFile(File file) {
		kamfireInstance.setGifStreamFromFile(file);
	}

	OnClickListener imageListener = new OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};

	private class LoadGif extends AsyncTask<Void, Boolean, Boolean> {
		ProgressDialog dialog;

		protected void onPreExecute() {
			dialog = ProgressDialog.show(FullScreenController.this, "", "Loading...");
		}

		protected Boolean doInBackground(Void... params) {
			loadGif();
			return true;
		}

		// onPostExecute displays the results of the AsyncTask.
		protected void onPostExecute(Boolean b) {
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			renderGif();

		}

	}
}
